package com.zenithmind.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.mall.pojo.entity.ProductStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 商品统计Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-07-03
 */
@Mapper
public interface ProductStatisticsMapper extends BaseMapper<ProductStatistics> {

    /**
     * 增加浏览量
     * 
     * @param productId 商品ID
     * @return 更新行数
     */
    @Update("UPDATE mall_product_statistics SET view_count = view_count + 1, " +
            "update_time = NOW() WHERE product_id = #{productId}")
    int increaseViewCount(@Param("productId") String productId);

    /**
     * 增加销量
     * 
     * @param productId 商品ID
     * @param quantity 销售数量
     * @return 更新行数
     */
    @Update("UPDATE mall_product_statistics SET sales_count = sales_count + #{quantity}, " +
            "update_time = NOW() WHERE product_id = #{productId}")
    int increaseSalesCount(@Param("productId") String productId, 
                          @Param("quantity") Integer quantity);

    /**
     * 增加收藏量
     * 
     * @param productId 商品ID
     * @return 更新行数
     */
    @Update("UPDATE mall_product_statistics SET favorite_count = favorite_count + 1, " +
            "update_time = NOW() WHERE product_id = #{productId}")
    int increaseFavoriteCount(@Param("productId") String productId);

    /**
     * 减少收藏量
     * 
     * @param productId 商品ID
     * @return 更新行数
     */
    @Update("UPDATE mall_product_statistics SET favorite_count = GREATEST(favorite_count - 1, 0), " +
            "update_time = NOW() WHERE product_id = #{productId}")
    int decreaseFavoriteCount(@Param("productId") String productId);

    /**
     * 更新评分
     * 
     * @param productId 商品ID
     * @param averageRating 平均评分
     * @param reviewCount 评价数量
     * @param positiveReviewCount 好评数量
     * @return 更新行数
     */
    @Update("UPDATE mall_product_statistics SET average_rating = #{averageRating}, " +
            "review_count = #{reviewCount}, positive_review_count = #{positiveReviewCount}, " +
            "update_time = NOW() WHERE product_id = #{productId}")
    int updateRating(@Param("productId") String productId,
                    @Param("averageRating") Double averageRating,
                    @Param("reviewCount") Integer reviewCount,
                    @Param("positiveReviewCount") Integer positiveReviewCount);
}
